package tools;

import javax.swing.*;
import javax.swing.border.Border;
import java.awt.*;
import java.awt.event.*;

/**
 * <p> TODU </P>
 *
 * @author yeager
 * @Version V1.1.1.0
 * @date 2023/11/24 9:52
 */
public class ToolBarComboBox extends JComboBox {
    // Static fields.

    /** The button will always have this size. */
    Dimension DEFAULT_SIZE = new Dimension(60, 30);
    /** The border width of the button. */
    private static final int BORDER = 4;
    /** The empty border of the button. */
    private static final Border EMPTY_BORDER = BorderFactory.createEmptyBorder(BORDER, BORDER, BORDER, BORDER);
    /** The border of the button when the mouse is over it. */
    private static final Border LINE_BORDER = BorderFactory.createCompoundBorder(
            BorderFactory.createCompoundBorder(
                    BorderFactory.createEmptyBorder(BORDER - 2, BORDER - 2, BORDER - 2, BORDER - 2),
                    BorderFactory.createLineBorder(Color.gray)),
            BorderFactory.createEmptyBorder(1, 1, 1, 1));
    ActionListener listener;

    // Fields.

    // Constructors.

    /**
     * Constructs an icon button with the given action.
     *
     * @param	action 		The action that will be performed when this button is clicked.
     */
    public ToolBarComboBox(ActionListener listener,int Width)
    {
        DEFAULT_SIZE = new Dimension(Width, 30);
        setDimensions();
        addMouseListener(new ClickListener());
        setBorder(EMPTY_BORDER);
        setOpaque(false);
        //setOpaque(true);
        this.listener=listener;
        addItemListener(new ItemListener() {

            @Override
            public void itemStateChanged(ItemEvent arg0) {

                ActionEvent actionEvent = new ActionEvent(ToolBarComboBox.this, ActionEvent.ACTION_PERFORMED, String.valueOf(ToolBarComboBox.this.getSelectedItem()), 0);
                listener.actionPerformed(actionEvent);
            }});

    }

    // Getters / Setters.

    // Private classes.

    /**
     * This mouse listener performs the action when the mouse is clicked.
     */
    private class ClickListener extends MouseAdapter
    {

        // Overwritten methods of MouseAdapter.

        public void mouseClicked(MouseEvent mouseEvent)
        {
            // Create the action event.

            //ActionEvent actionEvent = new ActionEvent(ToolBarComboBox.this, ActionEvent.ACTION_PERFORMED, getToolTipText(), mouseEvent.getModifiers());
            //listener.actionPerformed(actionEvent);
        }

        public void mouseEntered(MouseEvent mouseEvent)
        {
            setBorder(LINE_BORDER);
        }

        public void mouseExited(MouseEvent mouseEvent)
        {
            setBorder(EMPTY_BORDER);
        }

    }

    // Private metods.

    /**
     * Sets the preferred, maximum and minimum size of the button.
     */
    private void setDimensions()
    {

        setPreferredSize(DEFAULT_SIZE);
        setMaximumSize(DEFAULT_SIZE);
        setMinimumSize(DEFAULT_SIZE);

    }


}
